<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <title>$scope的基本使用方法</title>
</head>
<body>
<div ng-app="tinywan">
    <div ng-controller="ctrl">
        商品名称：{{goods.data.title}}<br/>
        价格：{{goods.data.price}}（元）<br/>
        数量：{{goods.data.num}}（件）<br/>
        总计：{{goods.data.num*goods.data.price}}（元）<br/>
        <button ng-click="goods.add()">增加</button>
        <button ng-click="goods.reduce()">减少</button>
    </div>
</div>

<script>
    var module = angular.module('tinywan', []);
    module.controller('ctrl', ['$scope', function ($scope) {
        $scope.goods = {
            data: {'title': 'apple mac', 'price': 300, 'num': 0},
            add: function () {
                $scope.goods.data.num = Math.min(++$scope.goods.data.num, 6);
            },
            reduce: function () {
                $scope.goods.data.num = Math.max(--$scope.goods.data.num, 0);
            }
        }
    }]);
</script>
</body>
</html>


